package whf.framework.resource.msg;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import whf.framework.config.ApplicationConfig;
import whf.framework.resource.ResourceFactory;

/**
 * @author wanghaifeng
 *
 */
public class Messages {
	private final static String MESSAGE_BUNDLE_NAME = ApplicationConfig.getInstance().getValue("resource.messages.bundle-name");

	private final static ResourceBundle MESSAGE_BUNDLE = ResourceFactory.getResourceBundle(MESSAGE_BUNDLE_NAME);

	public static String getString(String key) {
		try {
			return MESSAGE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
	}

	public static String getString(String key, Object[] parameters) {
		String baseMsg;
		try {
			baseMsg = MESSAGE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
		return MessageFormat.format(baseMsg, parameters);
	}

	public static String getString(String key, Object parameter) {
		return getString(key, new Object[] { parameter });
	}

	public static void main(String[] args) throws Exception {
		System.out.println(getString("month.1")+":"+MESSAGE_BUNDLE_NAME);
	}
}
